package seismosurfer.util;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * A <a href="http://www.martinfowler.com/eaaDev/Notification.html">
 * Notification</a> implementation.
 * Collects together information about errors and 
 * other information in the domain layer and communicates 
 * it to the presentation.
 *
 */
public class Notification implements Serializable {
    
    private static final long serialVersionUID = -815534176467770879L;

    private Map errors = new HashMap();

    public Notification() {
    }
    
    /**
     * Stores an error message associated with a
     * data field.
     * 
     * @param field the name of the field
     * @param message the error message
     */
    public void put(String field, String message) {
        errors.put(field, new UserError(message));
    }
    
    /**
     * Gets an error message for the given field.
     * 
     * @param field the name of the field
     * @return a UserError object that contains 
     *         the error message
     */
    public UserError get(String field) {
        return (UserError) errors.get(field);
    }
    
    /**
     * Returns true if any error messages 
     * are stored in this object.
     * 
     * @return true if there are errors
     */
    public boolean hasErrors() {
        return !errors.isEmpty();
    }
    
    /**
     * Provides a reference to a
     * an Iterator of the data fields.
     * 
     * @return an Iterator of the data fields
     */
    public Iterator fieldsIterator() {
        Set fields = errors.keySet();

        return fields.iterator();
    }
    
    /**
     * Gets a Map of the field/error message pairs.
     * 
     * @return a Map of the field/error message pairs
     */
    public Map getErrors() {
        return errors;
    }

}
